// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CBL_FREELIST_H__
#define __INCLUDED_CBL_FREELIST_H__

#include "CBL_Defines.h"
#include "CBL_Debug.h"
#include <vector>

CB_SUBNAMESPACE_START(Memory)

class CFreeList
{
public:

	//! \brief	Creates a freelist for a given BlockSize.
	//!			Increase _BlocksPerList to reduce re-allocations (at the cost of using more memory).
	//!			You can also pre-allocate a certain size and throw an assert if this size is exceeded.
	//!
	//!			The data access looks as following:
	//!			m_pDataBlocks[0.._Prealloc][0.._BlocksPerList]
	//!
	//!			Note that the first array might be expanded, but the second array never does.
	//!			So it is totally safe to store a pointer to an item in the second array, as the address
	//!			will never change (in contrast to a stl vector).
	
	CFreeList(uint32 _BlockSizeByte, uint32 _BlocksPerList=100, uint32 _Prealloc=10, bool _AssertLimit=true);
	
	virtual ~CFreeList();
	
    //! \brief	Typesafe object allocation (with constructor call).
	template <typename TClassName>
	inline TClassName* Allocate();
	
	//! \brief	Just allocates memory without calling the constructor.
	//!			Use only if you know what you're doing.
	inline void* Allocate(size_t _Size);
	
    //! \brief	Typesafe object deallocation (with destructor call).
	template <typename TClassName>
	inline void Free(TClassName* _pObject);
	
	//! \brief	Just frees memory without calling the destructor.
	//!			Use only if you know what you're doing.
	inline void Free(void* _pObject);
	
	inline uint32 GetBlockSize() const { return m_BlockSizeByte; }
	
protected:
	
	BASELIB_API void AllocateBlocks();	
	BASELIB_API void* NextAvailableBlock();
	
private:
	
	typedef std::vector<uint8*> BlockVector;
	
	uint32		m_BlockSizeByte;				// Size of blocks stored per list
	uint32		m_BlocksPerList;				// Number of blocks stored per list
	
	uint8		*m_pNextFreeBlock;				// pointer to the next regulat allocation block
	uint32		m_FreeBlockIndex;
	uint32		m_Capacity;
	uint32		m_ReallocationSize;
	bool		m_AssertLimit;
	
	BlockVector	m_FreeBlocks;					// stack of pointers to free alocation blocks
	BlockVector	m_DataBlocks;					// vector of memory chunks
};

#include "CBL_FreeList.hpp"

CB_SUBNAMESPACE_END

#endif